var t = require("../../../utils/qiniuUploader"), e = getApp();

Page({
    data: {
        title: "",
        coverImage: "/images/add/ic_teletext_default.png",
        data: [],
        coverHeight: 240
    },
    titleBlur: function(t) {
        this.setData({
            title: t.detail.value
        });
    },
    clickAddCover: function(t) {
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                wx.navigateTo({
                    url: "uploadCover/uploadCover?src=" + t.tempFilePaths[0]
                });
            }
        });
    },
    clickAddImage: function(t) {
        var a = this;
        wx.chooseImage({
            count: 9,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                for (var i = t.tempFilePaths, o = a.data.data, n = 0; n < i.length; n++) {
                    var l = e.util.getQiniuFileName(a.data.data.length), s = o.length, c = {
                        type: 1,
                        image: i[n],
                        fileName: l,
                        index: s,
                        state: 0
                    };
                    o = o.concat(c), a.uploadImageFromQiniu(i[n], l, s);
                }
                a.setData({
                    data: o,
                    coverHeight: 240
                });
            }
        });
    },
    clickAddText: function(t) {
        wx.navigateTo({
            url: "addTuwenText/addTuwenText"
        });
    },
    clickUp: function(t) {
        var e = t.currentTarget.dataset.index, a = this.data.data, i = a[e];
        a.splice(e, 1), a.splice(e - 1, 0, i), this.setData({
            data: a
        });
    },
    clickDel: function(t) {
        var e = t.currentTarget.dataset.index, a = this.data.data;
        a[e];
        a.splice(e, 1), this.setData({
            data: a
        });
    },
    clickConfirm: function(t) {
        var a = this.data.title;
        if (0 != a.length) {
            var i = this.data.coverImage;
            if ("/images/add/ic_teletext_default.png" != i) {
                var o = this.data.data;
                if (0 != o.length) {
                    for (var n = [], l = 0; l < o.length; l++) {
                        var s = o[l];
                        if (2 == s.type) n = n.concat({
                            type: 2,
                            content: s.text
                        }); else {
                            if (1 != s.state) return void e.util.showToast("请等照片上传完");
                            n = n.concat({
                                type: 1,
                                content: s.fileName
                            });
                        }
                    }
                    var c = JSON.stringify(n);
                    e.network.uploadFile({
                        url: e.url.addTeletext,
                        filePath: i,
                        name: "file",
                        formData: {
                            title: a,
                            content: c
                        },
                        before: function() {
                            wx.showLoading({
                                title: "正在发布"
                            });
                        },
                        success: function(t) {
                            if (wx.hideLoading(), 1 == t.result) {
                                e.util.showToast("发布成功");
                                var a = getCurrentPages();
                                a[a.length - 2].setData({
                                    needClose: !0
                                }), setTimeout(function() {
                                    wx.navigateBack({}), wx.showTabBar({});
                                }, 1500);
                            } else e.util.showToast(t.message);
                        },
                        fail: function() {
                            wx.hideLoading(), e.util.showToast("发布失败");
                        },
                        complete: function() {}
                    });
                } else e.util.showToast("请先添加图片或文字");
            } else e.util.showToast("请先选择封面图片");
        } else e.util.showToast("请输入标题");
    },
    uploadImageFromQiniu: function(a, i, o) {
        var n = this;
        t.upload(a, function(t) {
            var e = n.data.data;
            e[o].state = 1, n.setData({
                data: e
            });
        }, function(t) {
            console.log("error: " + t);
        }, {
            region: "ECN",
            uptoken: e.globalData.qiniuToken,
            uploadURL: "https://up.qbox.me",
            key: i,
            domain: "i.hunyu.co/"
        }, function(t) {});
    },
    onLoad: function(t) {
        e.util.updateQiniuToken();
        var a = t.avatar;
        console.log(a), a && this.setData({
            coverImage: a
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = e.util.getShareInfo();
        return {
            title: e.globalData.commonShareTitle,
            path: t[0],
            imageUrl: t[1],
            success: function(t) {},
            fail: function(t) {}
        };
    }
});